package boundary;

import javax.swing.JPanel;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import java.awt.Point;
import org.jdesktop.swingx.JXTitledSeparator;
import boundary.Home.stati;
import store.BaseStore;
import java.awt.GridBagConstraints;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.WindowConstants;

public class Opzioni extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JXTitledSeparator titleSeparator = null;
	private JCheckBox cb = null;
	private JButton okB = null;
	private JButton AnnullaB = null;
	private JLabel label = null;
	private JSpinner jSpinner = null;
	private JLabel jLabel = null;
	private Home home = null;
	
	/**
	 * @param owner
	 */
	public Opzioni(Home owner) {
		super(owner);
		home = owner;
		setModal(true);
		initialize();
		Point center = new Point((int)(owner.getLocationOnScreen().getX() + owner.getWidth()/2), (int)(owner.getLocationOnScreen().getY() + owner.getHeight()/2));
		setLocation(center.x - getWidth()/2, center.y - getHeight()/2);
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(280, 185);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Opzioni");
		this.setContentPane(getJContentPane());
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 2;
			gridBagConstraints21.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.gridy = 3;
			jLabel = new JLabel();
			jLabel.setText("minuti");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridy = 3;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 3;
			label = new JLabel();
			label.setText("Backup ogni");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridwidth = 3;
			gridBagConstraints1.gridy = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 2;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.gridy = 4;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 4;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridwidth = 3;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.gridy = 0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getTitleSeparator(), gridBagConstraints);
			jContentPane.add(getOkB(), gridBagConstraints2);
			jContentPane.add(getAnnullaB(), gridBagConstraints3);
			jContentPane.add(getCb(), gridBagConstraints1);
			jContentPane.add(label, gridBagConstraints4);
			jContentPane.add(getJSpinner(), gridBagConstraints11);
			jContentPane.add(jLabel, gridBagConstraints21);
			
			int s = BaseStore.getSettingsSalvataggi();
			if (s == 0) {
				getCb().setSelected(true);
				abilita(false);
			}
			else {
				abilita(true);
				getJSpinner().setValue(s);
			}
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes titleSeparator
	 * 
	 * @return org.jdesktop.swingx.JXTitledSeparator
	 */
	private JXTitledSeparator getTitleSeparator() {
		if (titleSeparator == null) {
			titleSeparator = new JXTitledSeparator();
			titleSeparator.setTitle("Impostazione backup automatico");
		}
		return titleSeparator;
	}
	
	/**
	 * This method initializes cb
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCb() {
		if (cb == null) {
			cb = new JCheckBox();
			cb.setText("Disabilita backup automatico");
			cb.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					abilita(!getCb().isSelected());
				}
			});
		}
		return cb;
	}
	
	/**
	 * This method initializes okB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkB() {
		if (okB == null) {
			okB = new JButton();
			okB.setText("Conferma");
			okB.addActionListener(this);
		}
		return okB;
	}
	
	/**
	 * This method initializes AnnullaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAnnullaB() {
		if (AnnullaB == null) {
			AnnullaB = new JButton();
			AnnullaB.setText("Annulla");
			AnnullaB.addActionListener(this);
		}
		return AnnullaB;
	}
	
	/**
	 * This method initializes jSpinner
	 * 
	 * @return javax.swing.JSpinner
	 */
	private JSpinner getJSpinner() {
		if (jSpinner == null) {
			jSpinner = new JSpinner();
		}
		return jSpinner;
	}
	
	private void abilita(boolean b) {
		label.setEnabled(b);
		jSpinner.setEnabled(b);
		jLabel.setEnabled(b);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == getAnnullaB())
			dispose();
		else if (e.getSource() == getOkB()) {
			int s = (Integer)getJSpinner().getValue();
			if (getCb().isSelected()) s = 0;
			if (s != BaseStore.getSettingsSalvataggi()) {
				BaseStore.setSettingsSalvataggi(s);
				home.main.createTimerTask(s);
				home.setStato(stati.DEFAULT);
			}
			dispose();
		}
	}
	
} //  @jve:decl-index=0:visual-constraint="0,0"
